<template>
  <div>
    <el-dialog
      :title="readonly ? '查看' : data.id ? '编辑' : '新增'"
      :visible.sync="show"
      :before-close="closeDialog"
      :close-on-click-modal="true"
      width="900px"
      custom-class="dialog-dliveryDetail user-dialog-css"
      center
    >
      <el-form ref="form" v-loading="loading" :model="form" label-position="left" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名">
              <span class="info-text">{{ data.name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份">
              <el-select v-model="form.ident" placeholder="请选择" :disabled="readonly">
                <el-option v-for="item in USER_IDENT" :key="item.value" :label="`${item.value} (${item.type})`" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="ID">
              <span class="info-text">{{ data.id }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="真实姓名">
              <span class="info-text">{{ form.realname ? form.realname : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号">
              <span class="info-text">{{ form.phone ? form.phone : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份ID">
              <span class="info-text">{{ form.officialId ? form.officialId : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <span class="info-text">{{ form.email ? form.email : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校">
              <span class="info-text">{{ form.school ? form.school : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年级">
              <span class="info-text">{{ form.grade ? form.grade : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级">
              <span class="info-text">{{ form.class ? form.class : '用户未填写' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span v-if="!readonly" slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { userManagement } from '@/api/userManagement'
import { USER_IDENT } from '@/constants/user.constants'
export default {
  name: 'ListDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      loading: false,
      USER_IDENT
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.form.ident = this.data.ident
        this.getData()
      }
    }
  },
  methods: {
    async getData() {
      const params = {
        name: this.data.name,
        sessionId: this.$store.state.user.session_id
      }
      this.loading = true
      const result = await userManagement.getUserinfo(params)
      this.form = { ...this.form, ...result.data.userinfo }
      this.loading = false
    },
    submit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true
          await userManagement.updateIdent({ name: this.data.name, ident: this.form.ident, sessionId: this.$store.state.user.session_id })
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.loading = false
          this.afterSubmit()
        } else {
          return false
        }
      })
    },
    afterSubmit() {
      this.$emit('afterSubmit')
    },
    closeDialog() {
      this.$emit('closeDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input.is-disabled .el-input__inner {
    color: #606266;
    background-color: #fff;
}
::v-deep .el-dialog--center .el-dialog__body {
    padding: 25px 60px 25px;
}
.info-text {
    font-size: 16px;
    font-weight: 500;
}
</style>
